package com.bite.demo.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*
* 接口文档:
* 登录界面:
url: /user/login
请求方式: POST
接口描述: 判断用户名和密码是否正确
参数: String userName, String passwords
返回Content-Type: text/html
true 登陆成功
false 登陆失败
*
* 用户登录信息:
url: /user/getUserInfo
请求方式: GET
接口描述: 从session中拿到用户信息
参数:HttpSession session
返回Content-Type: text/html 返回用户信息



1. 确定路径
2. 确定参数
3. 参数校验
4. 逻辑实现(涉及到登陆, 要保存session
5. 确定返回值

*/
@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/login")
    public Boolean login(String userName, String passwords, HttpSession session){
        /*if(userName == null || passwords == null){
            return false;
        }*/
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(passwords)){
            return false;
        }

        if("zhangsan".equals(userName) && "123456".equals(passwords)){
            session.setAttribute("userName", userName);
            return true;
        }
        return false;
    }

    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpSession session){
        if(session == null){
            return "未登录";
        }
        String userName = (String) session.getAttribute("userName");
        return userName;
    }
}
